package com.detrum.base;

import android.os.Environment;
import android.os.Handler;
import android.os.Looper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by abc on 2017/7/24.
 */

public class BaseStatic {
    public static String NetPath = "http://192.168.1.11/test/detrumDX.json";//http网络请求地址
    public static String MediaPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/detrum/cache";//本地媒体文件地址
    public static String ThumbPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/detrum/thumb";//本地MP4文件缩略图
    public static String jsonVersion="1";//json版本号
    public static int DEVICEID;// 设备Id
    public static String USERID = "";// 全局用户Id
    public static String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/detrum/download";//下载文件夹位置--设备版本文件
    public static String deviceName="设备型号";//当前设备名称
    public static String deviceVersion="";//当前设备版本
    public static String deviceNetVersion="";//网络设备版本
    public static String deviceNetPath;//设备新版本下载地址
    public static String deviceNetMessage;//设备新版本更新信息
    public static String deviceNetImagePath;//设备广告图片地址
    public static String deviceNetMsgPath;//设备说明文件路径--不是图片或字符串格式
    public static String deviceNetVerify;//设备更新安装包校验码；
    public static int deviceNetLength;//设备更新安装包长度；
    public static String deviceNetExplain;//设备说明文件；

    public static String deviceLocalPath;//设备本地文件地址

    public static String deviceUsbTempAdr;//usb设备更新文件临时地址
    public static String deviceUsbNetVersion;//usb设备网络版本
    public static String deviceUsbName;//usb设备名称
    public static String deviceUsbVersion;//usb设备当前版本
    public static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().toString();
    public static String appName="detrumDX";//应用名称
    public static String appVersion;//当前应用app版本号
    public static int appCode;//当前应用appCode
    public static String appNetVersion;//网络app版本号
    public static int appNetCode;//网络appCode
    public static String appNetPath;//app下载地址
    public static int appNetLength;//app更新文件长度
    public static String appNetMessage;//app新版本更新信息
    public static String appNetImagePath;//app广告图片地址
    public static String appNetMsgPath;//app说明文件路径--不是图片或字符串格式
    public static String appNetVerify;//app安装文件校验码

    public static int SCREEN_WIDTH,SCREEN_HEIGHT;

    public static String DHCP_LOCAL_IP;//手机本地wifiIP
    public static String DHCP_SERVICE_IP;//设备服务器wifiIP

    public static boolean videoisLaunch;//启动录像
    public static boolean pictureisLaunch; //启动截屏
    public static boolean isLaunch;//应用是否启动

    public static boolean isMainLaunch;//首页是否启动

    public final static int HeaderLen = 8;

    public static int showPhotoOrVideo = 0;

    public static StringBuffer stringBuffer;

    private static Handler handler = new Handler(Looper.getMainLooper());

    private static class Record implements Runnable {

        @Override
        public void run() {
            if(stringBuffer!=null&&stringBuffer.length()!=0){
                BufferedWriter out=null;
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    File dir = new File(SDCARD_ROOT + File.separator + "detrum" + File.separator+"recode"+ File.separator);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    String fileName = dir.toString() + File.separator  + paserTime(System.currentTimeMillis())+ ".txt";
                    File recordFile = new File(fileName);
                    try {
                        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(recordFile, true), Charset.defaultCharset()));
                        out.write(stringBuffer.toString() + "\r\n");

                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if(out!=null){
                            try {
                                out.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                stringBuffer=null;
                //handler.postDelayed(this,60000);
            }
        }
    }

    private static Record record = new Record();

    /**
     * 运行关键节点记录
     * @param msg
     */
    static int num;
    public static void savaInfoToSD(String msg) {
        if(stringBuffer==null){
            stringBuffer=new StringBuffer();
        }
        stringBuffer.append(msg+"\n");
    }

    public static void RecordToSD(){
        new Thread(record).start();
    }

    private static String paserTime(long milliseconds) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(milliseconds));
    }

}
